package util

import (
	"encoding/json"
	"log"
	"net/http"
)

//type Ret struct {
//	Code int         `json:"code"`
//	Msg  string      `json:"msg"`
//	Data interface{} `json:"data,omitempty"`
//}

type Ret struct {
	Code  int         `json:"code"`
	Msg   string      `json:"msg"`
	Data  interface{} `json:"data,omitempty"`
	Rows  interface{} `json:"rows,omitempty"`
	Total interface{} `json:"total,omitempty"`
}

type ReturnJson struct{}
type ReturnBool struct{}

func (ReturnJson) ReturnJsonStr(writer http.ResponseWriter, code int, msg string, data interface{}) {
	// 初始化结构体
	CommonReturn := Ret{
		Code: code,
		Msg:  msg,
		Data: data,
	}
	StrMsg, err := json.Marshal(CommonReturn)
	if err != nil {
		log.Println(err.Error())
	}
	writer.Write([]byte(StrMsg))
}

func (ReturnJson) ReturnJsonList(writer http.ResponseWriter, code int, data interface{}, total interface{}) {
	// 初始化结构体
	CommonReturn := Ret{
		Code:  code,
		Rows:  data,
		Total: total,
	}
	StrMsg, err := json.Marshal(CommonReturn)
	if err != nil {
		log.Println(err.Error())
	}
	writer.Write([]byte(StrMsg))
}
